package com.credithc.signature.manager.controller;

import com.credithc.baseserv.core.codes.CoreCodes;
import com.credithc.baseserv.core.message.Message;
import com.credithc.baseserv.core.message.MessageFactory;
import com.credithc.baseserv.pageHelper.PageInfo;
import com.credithc.signature.common.po.Channel;
import com.credithc.signature.common.po.SignatureLogExt;
import com.credithc.signature.common.ro.facade.ReSignData;
import com.credithc.signature.common.ro.manager.RequestLogRo;
import com.credithc.signature.common.ro.manager.SignLogRo;
import com.credithc.signature.common.service.ChannelService;
import com.credithc.signature.common.service.SignatureLogExtService;
import com.credithc.signature.common.service.SignatureLogService;
import com.credithc.signature.common.utils.DateUtil;
import com.credithc.signature.common.vo.manager.DictionaryVo;
import com.credithc.signature.common.vo.manager.RequestLogVo;
import com.credithc.signature.common.vo.manager.SignLogPreviewVo;
import com.credithc.signature.common.vo.manager.SignLogVo;
import com.credithc.signature.manager.client.SignatureFeignClient;
import com.credithc.signature.manager.service.AuthorizationService;
import com.credithc.signature.manager.service.SignLogService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 描 述: 查询请求记录Controller<br/>
 * 作 者: JingBo Hu<br/>
 * 创 建：2019/5/24 16:55<br/>
 * 版 本：v1.0.0<br>
 */
@RestController
@RequestMapping("/manager/scs/v2")
@Slf4j
public class RequestLogController {


    @Autowired
    MessageFactory messageFactory;

    @Autowired
    SignatureLogService requestLogService;

    @Autowired
    SignatureLogExtService signatureLogExtService;

    @Autowired
    private SignLogService signLogService;

    @Autowired
    ChannelService channelService;

    @Autowired
    SignatureFeignClient signatureFeignClient;

    @Autowired
    AuthorizationService authorizationService;

    /**
     * 查询所有
     *
     * @param requestLogRo
     * @return
     */
    @PostMapping("/requestLog/list")
    public Message queryAll(@RequestHeader("token") String token, @RequestBody @Valid RequestLogRo requestLogRo, HttpServletRequest request) {
        try {
            Date startTime = requestLogRo.getCreateTime();
            Date endTime = requestLogRo.getUpdateTime();
            if (startTime == null) {
                return messageFactory.getInstance(CoreCodes.CORE_SYS_ERROR, "开始时间不允许为空");
            }
            if (endTime == null) {
                endTime = new Date();
            }
            long l = DateUtil.betweenMonths(startTime, endTime);
            if (l > 1) {
                return messageFactory.getInstance(CoreCodes.CORE_SYS_ERROR, "时间跨度不能超过两个月");
            }
            RequestLogRo newRequestLogRo = null;
            try {
                newRequestLogRo = authorizationService.authRequestLog(requestLogRo, token, request);
            } catch (Exception e) {
                return messageFactory.getInstance(CoreCodes.CORE_SUCCESS, e.getMessage());
            }
            PageInfo<RequestLogVo> pageInfo = requestLogService.queryAll(newRequestLogRo);
            Map<String, PageInfo<RequestLogVo>> map = new HashMap<>(16);
            map.put("pageInfo", pageInfo);
            return messageFactory.getInstance(CoreCodes.CORE_SUCCESS, map);
        } catch (Exception e) {
            log.error("获取授权时发生异常", e);
            return messageFactory.getInstance(CoreCodes.CORE_SYS_ERROR, "获取授权时发生异常");
        }
    }

    /**
     * 查询所有签章记录
     *
     * @param signLogRo
     * @return
     */
    @GetMapping("/signLog/list")
    public Message querySignLogAllManager(SignLogRo signLogRo, @RequestHeader String token) {
        Date startTime = signLogRo.getStartTime();
        Date endTime = signLogRo.getEndTime();
        if (startTime == null) {
            return messageFactory.getInstance(CoreCodes.CORE_SYS_ERROR, "开始时间不允许为空");
        }
        if (endTime == null) {
            endTime = new Date();
        }
        long l = DateUtil.betweenMonths(startTime, endTime);
        if (l > 1) {
            return messageFactory.getInstance(CoreCodes.CORE_SYS_ERROR, "时间跨度不能超过两个月");
        }
        PageInfo<SignLogVo> pageInfo = signLogService.querySignLogAll(signLogRo, token);
        Map<String, PageInfo> map = new HashMap<>(16);
        map.put("pageInfo", pageInfo);
        return messageFactory.getInstance(CoreCodes.CORE_SUCCESS, map);
    }

    /**
     * 签章记录预览接口
     *
     * @param id
     * @return
     */
    @GetMapping("/signLog/preview/{id}")
    public Message querySignLogPreview(@PathVariable Long id, String createTime) {
        Date dateFromStr = DateUtil.getDateFromStr(createTime);
        SignLogPreviewVo signLogPreviewVo = signLogService.querySignLogPreview(id, dateFromStr);
        Map<String, SignLogPreviewVo> map = new HashMap<>(16);
        map.put("vo", signLogPreviewVo);
        return messageFactory.getInstance(CoreCodes.CORE_SUCCESS, map);
    }

    /**
     * 查询单条
     *
     * @param requestLogRo
     * @return
     */
    @GetMapping("/requestLog/one")
    public Message queryOne(RequestLogRo requestLogRo) {
        RequestLogVo requestLogVo = requestLogService.queryOne(requestLogRo.getId(), requestLogRo.getDate());
        Map<String, RequestLogVo> map = new HashMap<>(16);
        map.put("vo", requestLogVo);
        return messageFactory.getInstance(CoreCodes.CORE_SUCCESS, map);
    }

    /**
     * 查看合同
     *
     * @return
     */
    @GetMapping("/requestLog/contract/{contractUri}")
    public Message lookContract(@PathVariable String contractUri, String channelNo) {
        RequestLogVo requestLogVo = requestLogService.lookContract(contractUri, channelNo);
        Map<String, RequestLogVo> map = new HashMap<>(16);
        map.put("vo", requestLogVo);
        return messageFactory.getInstance(CoreCodes.CORE_SUCCESS, map);
    }

    /**
     * 查询签章状态
     *
     * @param
     * @return
     */
    @PostMapping("/requestLog/signStatus/list")
    public Message querySignStatus() {
        List<DictionaryVo> vos = requestLogService.querySignStatus();
        Map map = new HashMap<>(16);
        map.put("list", vos);
        return messageFactory.getInstance(CoreCodes.CORE_SUCCESS, map);
    }

    /**
     * 按天统计请求记录数据
     *
     * @return
     */
    @GetMapping("/requestLog/count/day")
    public Message dayCount() {
        Integer i = requestLogService.queryDayCount();
        return messageFactory.getInstance(CoreCodes.CORE_SUCCESS, i);
    }
}
